Node.js 最新版本特性

1. Node.js 简介和特性

Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行时环境,它允许开发者使用 JavaScript 编写服务器端应用程序。Node.js 的主要特性包括:

  • 非阻塞 I/O: 通过事件驱动和异步 I/O 操作,Node.js 能够高效处理大量并发连接。
  • 单线程: Node.js 使用单线程模型,但在处理 I/O 操作时不会阻塞主线程。
  • 跨平台: Node.js 可以在多种操作系统上运行,包括 Windows、macOS 和 Linux。

2. 环境搭建和版本管理(nvm)

2.1 安装 Node.js

首先,你需要安装 Node.js。你可以从 Node.js 官方网站 下载并安装适合你操作系统的版本。

2.2 使用 nvm 管理 Node.js 版本

nvm(Node Version Manager)是一个用于管理多个 Node.js 版本的工具。你可以通过以下命令安装 nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

安装完成后,你可以使用 nvm 安装和管理不同版本的 Node.js:

nvm install 16.0.0
nvm use 16.0.0

3. 创建第一个 Node.js 应用

3.1 初始化项目

创建一个新的目录并初始化一个新的 Node.js 项目:

mkdir my-first-node-app
cd my-first-node-app
npm init -y

3.2 编写第一个应用

创建一个 index.js 文件,并编写以下代码:

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

server.listen(3000, '127.0.0.1', () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

3.3 运行应用

在终端中运行以下命令启动服务器:

node index.js

打开浏览器并访问 http://127.0.0.1:3000/,你应该会看到 "Hello, World!" 的输出。

4. 模块系统(CommonJS 和 ES Modules)

4.1 CommonJS 模块

Node.js 默认使用 CommonJS 模块系统。你可以通过 require 和 module.exports 来导入和导出模块:

// math.js
module.exports = {
  add: (a, b) => a + b,
  subtract: (a, b) => a - b,
};

// index.js
const math = require('./math');
console.log(math.add(5, 3)); // 输出: 8

4.2 ES Modules

从 Node.js 12 开始,你可以使用 ES Modules 语法(import 和 export)。你需要在 package.json 中添加 "type": "module":

{
  "type": "module"
}

然后你可以使用 ES Modules 语法:

// math.js
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;

// index.js
import { add } from './math.js';
console.log(add(5, 3)); // 输出: 8

5. 包管理器(npm 和 yarn)

5.1 npm

npm 是 Node.js 的默认包管理器。你可以使用 npm install 安装依赖包:

npm install express

5.2 yarn

yarn 是另一个流行的包管理器,它提供了更快的安装速度和更可靠的依赖管理:

yarn add express

6. 事件循环和异步编程

6.1 事件循环

Node.js 的事件循环是其非阻塞 I/O 操作的核心。事件循环分为多个阶段,包括:

  • Timers: 执行 setTimeout 和 setInterval 回调。
  • I/O Callbacks: 执行大部分 I/O 操作的回调。
  • Idle, Prepare: 内部使用。
  • Poll: 检索新的 I/O 事件。
  • Check: 执行 setImmediate 回调。
  • Close Callbacks: 执行 close 事件的回调。

6.2 异步编程

Node.js 提供了多种异步编程模式,包括回调函数、Promise 和 async/await。

// 回调函数
const fs = require('fs');
fs.readFile('file.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

// Promise
const util = require('util');
const readFilePromise = util.promisify(fs.readFile);
readFilePromise('file.txt', 'utf8')
  .then(data => console.log(data))
  .catch(err => console.error(err));

// async/await
async function readFileAsync() {
  try {
    const data = await readFilePromise('file.txt', 'utf8');
    console.log(data);
  } catch (err) {
    console.error(err);
  }
}
readFileAsync();

7. 回调函数、Promise 和 async/await

7.1 回调函数

回调函数是 Node.js 中最早的异步编程模式。它通过将函数作为参数传递给另一个函数来实现异步操作:

function fetchData(callback) {
  setTimeout(() => {
    callback('Data fetched');
  }, 1000);
}

fetchData(data => {
  console.log(data);
});

7.2 Promise

Promise 是 ES6 引入的一种异步编程模式,它提供了更清晰的错误处理和链式调用:

function fetchData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve('Data fetched');
    }, 1000);
  });
}

fetchData()
  .then(data => console.log(data))
  .catch(err => console.error(err));

7.3 async/await

async/await 是 ES8 引入的一种异步编程模式,它使得异步代码看起来更像同步代码:

async function fetchDataAsync() {
  try {
    const data = await fetchData();
    console.log(data);
  } catch (err) {
    console.error(err);
  }
}

fetchDataAsync();

8. 流(Streams)和缓冲区(Buffers)

8.1 流

流是 Node.js 中处理数据的一种方式,它允许你以块的形式处理数据,而不是一次性加载整个数据集:

const fs = require('fs');
const readableStream = fs.createReadStream('file.txt');

readableStream.on('data', chunk => {
  console.log(`Received ${chunk.length} bytes of data.`);
});

readableStream.on('end', () => {
  console.log('Finished reading file.');
});

8.2 缓冲区

缓冲区是 Node.js 中处理二进制数据的一种方式。你可以使用 Buffer 类来创建和操作缓冲区:

const buffer = Buffer.from('Hello, World!', 'utf8');
console.log(buffer.toString('utf8')); // 输出: Hello, World!

9. 文件系统操作

Node.js 提供了丰富的文件系统操作 API,包括读取、写入、删除文件等:

const fs = require('fs');

// 读取文件
fs.readFile('file.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

// 写入文件
fs.writeFile('file.txt', 'Hello, World!', 'utf8', err => {
  if (err) throw err;
  console.log('File written successfully.');
});

// 删除文件
fs.unlink('file.txt', err => {
  if (err) throw err;
  console.log('File deleted successfully.');
});

10. 网络编程(HTTP/HTTPS 模块)

Node.js 提供了 http 和 https 模块,用于创建 HTTP 和 HTTPS 服务器:

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

server.listen(3000, '127.0.0.1', () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

11. Express.js 框架入门

Express.js 是一个流行的 Node.js 框架,用于构建 Web 应用程序和 API:

npm install express
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(3000, () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

12. 路由和中间件

12.1 路由

Express.js 提供了强大的路由功能,允许你定义不同路径的处理函数:

app.get('/users', (req, res) => {
  res.send('List of users');
});

app.post('/users', (req, res) => {
  res.send('User created');
});

12.2 中间件

中间件是 Express.js 中用于处理请求和响应的函数。你可以使用中间件来添加日志记录、身份验证等功能:

app.use((req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next();
});

13. 模板引擎(EJS, Pug)

13.1 EJS

EJS 是一个简单的模板引擎,允许你在 HTML 中嵌入 JavaScript:

npm install ejs
app.set('view engine', 'ejs');

app.get('/', (req, res) => {
  res.render('index', { title: 'My App' });
});

13.2 Pug

Pug 是一个简洁的模板引擎,使用缩进来定义 HTML 结构:

npm install pug
app.set('view engine', 'pug');

app.get('/', (req, res) => {
  res.render('index', { title: 'My App' });
});

14. RESTful API 设计和实现

RESTful API 是一种基于 HTTP 协议的 API 设计风格。你可以使用 Express.js 来创建 RESTful API:

const express = require('express');
const app = express();

app.use(express.json());

let users = [];

app.get('/users', (req, res) => {
  res.json(users);
});

app.post('/users', (req, res) => {
  const user = req.body;
  users.push(user);
  res.status(201).json(user);
});

app.listen(3000, () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

15. 身份认证和授权

15.1 身份认证

你可以使用 Passport.js 来实现身份认证:

npm install passport passport-local
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;

passport.use(new LocalStrategy(
  (username, password, done) => {
    // 验证用户
    if (username === 'admin' && password === 'password') {
      return done(null, { id: 1, username: 'admin' });
    } else {
      return done(null, false, { message: 'Incorrect username or password.' });
    }
  }
));

app.use(passport.initialize());
app.use(passport.session());

app.post('/login', passport.authenticate('local'), (req, res) => {
  res.send('Logged in');
});

15.2 授权

你可以使用中间件来实现授权:

function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {
    return next();
  }
  res.redirect('/login');
}

app.get('/dashboard', ensureAuthenticated, (req, res) => {
  res.send('Dashboard');
});

16. SQL 数据库(MySQL, PostgreSQL)

16.1 MySQL

你可以使用 mysql 模块来连接和操作 MySQL 数据库:

npm install mysql
const mysql = require('mysql');
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'mydb'
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', (err, rows, fields) => {
  if (err) throw err;
  console.log('The solution is: ', rows[0].solution);
});

connection.end();

16.2 PostgreSQL

你可以使用 pg 模块来连接和操作 PostgreSQL 数据库:

npm install pg
const { Client } = require('pg');
const client = new Client({
  user: 'root',
  host: 'localhost',
  database: 'mydb',
  password: 'password',
  port: 5432,
});

client.connect();

client.query('SELECT $1::text as message', ['Hello world!'], (err, res) => {
  if (err) throw err;
  console.log(res.rows[0].message); // 输出: Hello world!
  client.end();
});

17. NoSQL 数据库(MongoDB)

你可以使用 mongoose 模块来连接和操作 MongoDB 数据库:

npm install mongoose
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mydb', { useNewUrlParser: true, useUnifiedTopology: true });

const userSchema = new mongoose.Schema({
  name: String,
  age: Number,
});

const User = mongoose.model('User', userSchema);

const user = new User({ name: 'John', age: 30 });
user.save((err) => {
  if (err) throw err;
  console.log('User saved successfully.');
});

18. ORM 和 ODM(Sequelize, Mongoose)

18.1 Sequelize

Sequelize 是一个流行的 ORM 库,用于操作 SQL 数据库:

npm install sequelize
npm install sqlite3
const { Sequelize, DataTypes } = require('sequelize');
const sequelize = new Sequelize('sqlite::memory:');

const User = sequelize.define('User', {
  name: DataTypes.STRING,
  age: DataTypes.INTEGER,
});

sequelize.sync().then(() => {
  return User.create({
    name: 'John',
    age: 30,
  });
}).then(user => {
  console.log(user